#ifndef LOADING_FUNCTIONS_H
#define LOADING_FUNCTIONS_H

#include <fstream>
#include "vector.h"

using namespace std;

bool littleEndian();
unsigned short toUShort(const char* bytes);
float toFloat(const char* bytes);
unsigned short readUShort(ifstream &input);
float readFloat(ifstream &input);
Vector readVec3f(ifstream &input);
int toInt(const char* bytes);
short toShort(const char* bytes);
int readInt(ifstream &input);
short readShort(ifstream &input);
short readShortOneByte(ifstream &input);

template<class T>
class auto_array {
	private:
		T* array;
		mutable bool isReleased;
	public:
		explicit auto_array(T* array_ = NULL) : array(array_), isReleased(false)
		{
		}

		auto_array(const auto_array<T> &aarray)
		{
			array = aarray.array;
			isReleased = aarray.isReleased;
			aarray.isReleased = true;
		}

		~auto_array()
		{
			if (!isReleased && array != NULL)
				delete[] array;
		}

		T* get() const
		{
			return array;
		}

		T &operator*() const
		{
			return *array;
		}

		void operator=(const auto_array<T> &aarray)
		{
			if (!isReleased && array != NULL)
				delete[] array;

			array = aarray.array;
			isReleased = aarray.isReleased;
			aarray.isReleased = true;
		}

		T* operator->() const
		{
			return array;
		}

		T* release()
		{
			isReleased = true;
			return array;
		}

		void reset(T* array_ = NULL)
		{
			if (!isReleased && array != NULL)
				delete[] array;

			array = array_;
		}

		T* operator+(int i)
		{
			return array + i;
		}

		T &operator[](int i)
		{
			return array[i];
		}
};

#endif //LOADING_FUNCTIONS_H